<!-- component中的组件会帮我们自动进行注册 -->
<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { artGetChannelsService, artDelChannelService } from '../../api/article'
import ChannelEdit from './components/ChannelEdit.vue' //导入组件-对话框

//loading组件
const loading = ref(false)
const dialog = ref()

//从服务器获取用户文章分类页数据
const channelList = ref([])
const getChannelList = async () => {
  loading.value = true //开启loading
  const res = await artGetChannelsService()
  loading.value = false //关闭loading
  channelList.value = res.data.data
  //console.log(channelList.value) //测试拿到了结果
}
getChannelList() //执行

//点击编辑按钮的事件
const onEditChannel = (row, $index) => {
  //console.log(row, $index)
  dialog.value.open(row, $index)
}

//点击删除按钮的事件
const onDelChannel = async (row) => {
  console.log(row)
  await ElMessageBox.confirm('你确认要删除该分类吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage.success('删除成功')
  getChannelList()
}

//点击添加分类的事件
const onAddChannel = () => {
  dialog.value.open({})
}

//对话框点确定后的方法--→更新数据
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <!-- 使用组件 -->
  <!-- 默认插槽中传入内容
       具名插槽中传入一个el按钮 #extra选中插槽-->
  <page-Container title="文章分类">
    <!-- 右上角空位加上一个按钮 -->
    <template #extra>
      <el-button @click="onAddChannel">添加分类</el-button></template
    >

    <!-- 内容部分加入el-table  以下表格部分-el-table表格 -->
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类别称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="100">
        <!-- row就是channelList的每一项item, $index 下标 -#default表插table内容部分-->
        <template #default="{ row, $index }">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <!--数据为空时显示，用的是el-table的插槽 -->
      <template #empty>
        <el-empty description="没有数据捏" />
      </template>
    </el-table>
    <!--使用组件Channeledit  -->
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </page-Container>
</template>

<style lang="scss" scoped></style>
